package com.black.web.app.store;

import com.black.app.domain.req.store.add.AppStoreCounponsAddReq;
import com.black.app.domain.req.store.edit.AppStoreCouponsEditReq;
import com.black.app.domain.req.store.query.AppStoreCouponsQueryReq;
import com.black.app.domain.resp.store.AppStoreCouponsResp;
import com.black.app.domain.store.AppStoreCoupons;
import com.black.app.service.store.AppStoreCouponsService;
import com.black.common.annotation.Log;
import com.black.common.core.controller.BaseController;
import com.black.common.core.domain.R;
import com.black.common.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Api(tags = "app-优惠券相关Api")
@RestController
@RequestMapping("/app/coupons")
public class AppStoreCouponsController extends BaseController {

    @Autowired
    private AppStoreCouponsService appStoreCouponsService;

//    @PreAuthorize("@ss.hasAppPermi()")
    @ApiOperation(value = "分页查询优惠券信息")
    @GetMapping("/list")
    public Object paginationQuery(AppStoreCouponsQueryReq req) {
        return appStoreCouponsService.paginationQuery(req);
    }

    @ApiOperation(value = "根据id查询")
    @GetMapping("/{id}")
    public R<AppStoreCouponsResp> findById(@PathVariable(value = "id") Long id) {
        AppStoreCouponsResp resp = this.appStoreCouponsService.findById(id);
        return R.ok(resp);
    }

    @Log(title = "优惠卷管理", businessType = BusinessType.INSERT)
//    @PreAuthorize("@ss.hasPermi('system:coupons:add')")
    @ApiOperation(value = "添加优惠券")
    @PostMapping
    public R add(@RequestBody @Valid AppStoreCounponsAddReq req) {
        return appStoreCouponsService.add(req) ? R.ok() : R.fail();
    }

    @Log(title = "优惠卷管理", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改优惠券")
    @PutMapping
    public R update(@RequestBody  @Valid AppStoreCouponsEditReq req) {
        return appStoreCouponsService.edit(req) ? R.ok() : R.fail();
    }

    @Log(title = "优惠卷管理", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除优惠券")
    @DeleteMapping("/{ids}")
    public R delete(@PathVariable(value = "ids") Long[] ids) {
        return appStoreCouponsService.deleteById(ids) ? R.ok() : R.fail();
    }
}
